package com.ccys.cms.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.ccys.cms.service.ModuleService;
import com.ccys.common.enums.CodeEnum;
import com.ccys.common.util.JedisUtil;
import com.ccys.common.util.Kit;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.web.base.ModuleDto;
import com.ccys.db.entity.SysModule;
import com.ccys.db.mapper.SysModuleMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.vo.web.base.ModuleListVo;
import com.ccys.db.vo.web.base.ModuleRouteVo;
import com.ccys.db.vo.web.base.ModuleVo;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.ccys.common.vo.ResponseResult.error;
import static com.ccys.common.vo.ResponseResult.success;


@Service
public class ModuleServiceImpl extends BaseServiceImpl<SysModule> implements ModuleService {

    @Resource
    private SysModuleMapper dao;



    @Override
    public ResponseResult queryModules( ModuleDto moduleDto) {
        PageDto.startPage();
        List<ModuleListVo> moduleListVos = dao.queryModules(moduleDto);
        PageInfo<ModuleListVo> info = new PageInfo<>(moduleListVos);
        return success(info);
    }

    @Override
    public void setCache() {
        JedisUtil.setex(CodeEnum.REFRESH_MODULE.msgOf(),
                CodeEnum.REFRESH_MODULE.codeOf(),
                CodeEnum.REFRESH_MODULE.msgOf(),
                CodeEnum.REFRESH_MODULE.codeOf());
    }

    @Override
    public void refreshCache() {
        List<ModuleVo> moduleVos = dao.queryAll();
        JedisUtil.setObj(CodeEnum.REDIS_SYS_MODULE.msgOf(),
                moduleVos,
                CodeEnum.REDIS_SYS_MODULE.codeOf());
    }

    @Override
    public ResponseResult getCache() {
        return success(JedisUtil.getObject(CodeEnum.REDIS_SYS_MODULE.msgOf(), CodeEnum.REDIS_SYS_MODULE.codeOf()));
    }

    @Override
    public ResponseResult oneClickGeneration( String jsonArray) {
        if (!Kit.isJsonArray(jsonArray)) {
            return error("请正确使用JSON格式数组");
        }
        final List<ModuleRouteVo> voList = JSONObject.parseArray(jsonArray, ModuleRouteVo.class);
        List<SysModule> mList = new ArrayList<>();
        for (int i = 0, size = voList.size(); i < size; i++) {
            final ModuleRouteVo v = voList.get(i);
            SysModule m = new SysModule();
            m.setId(IdUtil.randomUUID());
            m.setPath(v.getPath());
            m.setName(v.getName());
            m.setIcon(v.getIcon());
            m.setPid("0");
            m.setNumber(String.valueOf(i + 1));
            mList.add(m);
            if (v.getChildren() != null && v.getChildren().size() > 0) {
                children(mList, m.getId(), v.getChildren());
            }
        }
        SysModule m1 = new SysModule();
        m1.setDelFlag(0);
        this.dao.delete(m1);
        this.dao.batchSave(mList);
        return success();
    }

    private void children(List<SysModule> mList, String pid, List<ModuleRouteVo> list) {
        for (int i = 0, size = list.size(); i < size; i++) {
            final ModuleRouteVo c = list.get(i);
            SysModule m = new SysModule();
            m.setId(IdUtil.randomUUID());
            m.setPath(c.getPath());
            m.setName(c.getName());
            m.setIcon(c.getIcon());
            m.setPid(pid);
            m.setNumber(String.valueOf(i + 1));
            mList.add(m);
            if (c.getChildren() != null && c.getChildren().size() > 0) {
                children(mList, m.getId(), c.getChildren());
            }
        }
    }

}
